// pages/post_detail/post_detail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id: '',
    merchant_id: '',
    merchant: {},
    merchant_img: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      merchant_id: options.merchant_id
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var value = wx.getStorageSync('user_id');
    if (value && this.data.merchant_id) {
      this.getMerchantDetail();
      this.setData({
        user_id: value
      })
    } else {
      app.login()
      .then(app.doLogin)
      .then(app.checkAuth('scope.userInfo'))
      .then(app.getUserInfo)
      .then(app.setUserData)
      .then(this.getMerchantDetail)
      .catch(app.toLoad)
    }
  },

  getMerchantDetail: function () {
    let that = this;
    return new Promise((resolve, reject) => {
      wx.request({
        url: 'https://rtutrip.aihuodong.top/index/index/get_merchant_data',
        data: {
          merchant_id: that.data.merchant_id
        },
        success(res) {
          console.log(res.data)
          if (res.data.data.merchant.merchant_img) {
            var merchant_img = JSON.parse(res.data.data.merchant.merchant_img);
            that.setData({
              merchant_img: merchant_img
            })
          }
          that.setData({
            merchant: res.data.data.merchant,
          })
          resolve(res);
        }
      })
    })
  },
  showDialog: function () {
    this.setData({
      ishidden: !this.data.ishidden
    })
  },
  onShareAppMessage: function (res) {
    const _this = this
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: _this.data.merchant.merchant_name,
      path: '/pages/merchant_detail/merchant_detail?merchant_id=' + _this.data.merchant_id
    }
  }
})